package TextNor.Read;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 * Gán thẻ alpha cho file, những nsw nào là một từ trong từ điển thì bỏ thẻ nsw của nó.
 */
public final class ReadAll {

    Vector<ThreeDigital> level;
    private HashMap<String, String> charAr = new HashMap<String, String>();
    private HashMap<String, String> aswdAr = new HashMap<String, String>();
    private HashMap<String, String> unitMoneyAr = new HashMap<String, String>();
    private HashMap<String, String> greekAr = new HashMap<String, String>();

    public ReadAll(String inf, String ouf) throws ClassNotFoundException, SQLException, UnsupportedEncodingException, XMLStreamException, FileNotFoundException, IOException {

        System.out.println("package TextNor.Read; class ReadAll");

        setCharAr();
        setAswdAr();
        setGreekAr();

        setUnitMoneyAr();
        
        read(inf, ouf);
    }

    public void read(String inf, String ouf) throws XMLStreamException, IOException {
        long startTime = System.currentTimeMillis();

        FileInputStream fis = new FileInputStream(inf);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouf);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);
        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);
                    }
                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();
                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }

                    if (localName.equals("W")) {
                        try{



                        String atVal = xmlStreamReader.getAttributeValue(0);
                        if (atVal.equals("NTIM")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNTIM(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NDAT")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNDAT(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NDAY")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNDAY(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NMON")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNMON(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NNUM")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNNUM(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NTEL")) {
                            String nsw = xmlStreamReader.getElementText();
                            ArrayList<String> ar = new ArrayList<String>();
                            split(nsw, ar);

                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            for (int i = 0; i < ar.size(); i++) {
                                String ss = ar.get(i);
                                if (isNum(ss)) {
                                    xmlStreamWriter.writeStartElement("W");
                                    xmlStreamWriter.writeAttribute("NSW", "NDIG");

                                    xmlStreamWriter.writeAttribute("FULL", readNDIG(ss));
                                    xmlStreamWriter.writeEndElement();
                                } else {
                                    xmlStreamWriter.writeStartElement("W");
                                    xmlStreamWriter.writeAttribute("NSW", "DURA");
                                    xmlStreamWriter.writeAttribute("FULL", readDURA(ss));
                                    xmlStreamWriter.writeEndElement();
                                }

                            }

                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NFRC")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNFRC(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NSCR")) {
                            String nsw = xmlStreamReader.getElementText();
                            ArrayList<String> ar = new ArrayList<String>();
                            split(nsw, ar);

                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            for (int i = 0; i < ar.size(); i++) {
                                if (isNum(ar.get(i))) {
                                    xmlStreamWriter.writeStartElement("W");
                                    xmlStreamWriter.writeAttribute("NSW", "NDIG");
                                    xmlStreamWriter.writeAttribute("FULL", readNNUM(ar.get(i)));
                                    xmlStreamWriter.writeEndElement();
                                } else {
                                    xmlStreamWriter.writeStartElement("W");
                                    xmlStreamWriter.writeAttribute("NSW", "DURA");
                                    xmlStreamWriter.writeAttribute("FULL", readDURA(ar.get(i)));
                                    xmlStreamWriter.writeEndElement();
                                }
                            }
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NRGN")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNRGN(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NADD")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNADD(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NSIG")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNSIG(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }

                        if (atVal.equals("ASWD")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readASWD(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("SEQN")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readSEQN(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("EXPN")) {
                            String full = xmlStreamReader.getAttributeValue(1);
                            String nsw = xmlStreamReader.getElementText();

                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("GREK")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readGREK(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("PUNC")) {
                            int atnum = xmlStreamReader.getAttributeCount();
                            if (atnum == 2) {
                                String full = xmlStreamReader.getAttributeValue(1);
                                String nsw = xmlStreamReader.getElementText();
                                xmlStreamWriter.writeStartElement(localName);
                                xmlStreamWriter.writeAttribute("NSW", atVal);
                                xmlStreamWriter.writeAttribute("FULL", full);
                                xmlStreamWriter.writeCharacters(nsw);
                                xmlStreamWriter.writeEndElement();
                            } else {
                                String nsw = xmlStreamReader.getElementText();
                                String full = readPUNC(nsw);
                                xmlStreamWriter.writeStartElement(localName);
                                xmlStreamWriter.writeAttribute("NSW", atVal);
                                xmlStreamWriter.writeAttribute("FULL", full);
                                xmlStreamWriter.writeCharacters(nsw);
                                xmlStreamWriter.writeEndElement();
                            }

                        }
                        if (atVal.equals("URLE")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readURLE(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("MONY")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readMONY(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("DURA")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readDURA(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        if (atVal.equals("NONE")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = readNONE(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            xmlStreamWriter.writeAttribute("FULL", full);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                        }
                        catch(Exception e){
                        }
                    }

                    if (localName.equals("SPLIT")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    break;


                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();

                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    if (endLocalName.equals("SPLIT")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }
        xmlStreamWriter.writeEndElement();

        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();

        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian gan the other la: " + time);

    }

    public void setCharAr() throws FileNotFoundException, UnsupportedEncodingException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("data//read//Char.db"), "UTF8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, "|");
            String c = st.nextToken();
            String r = st.nextToken();
            charAr.put(c, r);
        }

    }

    public void setGreekAr() throws FileNotFoundException, UnsupportedEncodingException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("data//read//greek.db"), "UTF8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, "|");
            String c = st.nextToken();
            String r = st.nextToken();
            greekAr.put(c, r);
        }
    }

    public void setAswdAr() throws UnsupportedEncodingException, FileNotFoundException  {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("data//read//aswd.db"), "UTF8"));
        String line = null;
        try{
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, "|");
            String a = st.nextToken();
            System.out.println(a);
            String r = st.nextToken();
            charAr.put(a, r);
        }
        }catch(Exception e){
        }
    }

    public void setUnitMoneyAr() throws FileNotFoundException, UnsupportedEncodingException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("data//read//unitMoney.db"), "UTF8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, "|");
            String l = st.nextToken();
            String r = st.nextToken();
            unitMoneyAr.put(l, r);
        }
    }

    public String readNDIG(String nsw) {
        String full = "";
        for (int i = 0; i < nsw.length(); i++) {
            String subStr = nsw.substring(i, i + 1);
            full += charAr.get(subStr) + " ";
        }
        return full;
    }

    public String readNFRC(String nsw) {
        System.out.println("NFRC: "+nsw);
        String full = "";
        String[] sp = nsw.split("/");
        full = readNum(sp[0]) + " phần " + readNum(sp[1]);
        return full;
    }

    public String readNSCR(String nsw) {
        String full = "";
        return full;
    }

    public String readNRGN(String nsw) {
        System.out.println("NRGN: "+nsw);
        String full = "";
        String[] sp = nsw.split("\\p{Pd}");
        full = readNum(sp[0]) + " đến " + readNum(sp[1]);
        return full;
    }

    public String readNADD(String nsw) {
        String full = "";
        String[] sp = nsw.split("/");
        full = readNum(sp[0]);
        for (int i = 1; i < sp.length; i++) {
            full = full + " trên " + sp[i];
        }
        return full;
    }

    public String readNSIG(String nsw) {
        String full = "";
        if (nsw.equals("2")) {
            full = "vuông";
        } else {
            full = "khối";
        }
        return full;
    }

    public String readASWD(String nsw) {
        nsw = nsw.toLowerCase();
        String full = "";
        if (aswdAr.containsKey(nsw)) {
            full = aswdAr.get(nsw);
        } else {
            full = "";
        }

        return full;
    }

    public String readSEQN(String nsw) {
        String full = "";
        nsw = nsw.toLowerCase();
        full = charAr.get(nsw.substring(0, 1));
        for (int i = 1; i < nsw.length(); i++) {
            full = full + " " + charAr.get(nsw.substring(i, i + 1));
        }
        return full;
    }

    public String readGREK(String nsw) {
        String full = "";
        full = greekAr.get(nsw);
        return full;
    }

    public String readPUNC(String nsw) {
        String full = "";
        if(nsw.equals("...")){
            return "ba chấm";
        }
        if (charAr.containsKey(nsw.substring(0, 1))) {
            full = charAr.get(nsw.substring(0, 1));
        } else {
            full = "";
        }

        for (int i = 1; i < nsw.length(); i++) {
            if (charAr.containsKey(nsw.substring(0, 1))) {
                full = full + " " + charAr.get(nsw.substring(i, i + 1));
            } else {
                full = full + "";
            }

        }
        return full;
    }

    public String readURLE(String nsw) {
        String full = "";
        full = charAr.get(nsw.substring(0, 1));
        for (int i = 1; i < nsw.length(); i++) {
            full = full + " " + charAr.get(nsw.substring(i, i + 1));
        }
        return full;
    }

    public String readMONY(String nsw) {
        String full = "";
        String[] ar = new String[2];
        Pattern SPLIT = Pattern.compile("(\\d\\p{L})|(\\p{L}\\d)");
        Matcher m = SPLIT.matcher(nsw);
        int lastIndex = 0;
        if (m.find()) {
            String nsw1 = nsw.substring(lastIndex, m.start() + 1);
            ar[0] = nsw1;
            String nsw2 = nsw.substring(m.end() - 1);
            ar[1] = nsw2;
        }
        if (isNum(ar[0])) {
            full = readNNUM(ar[0]) + " " + unitMoneyAr.get(ar[1]);
        } else {
            full = readNNUM(ar[1]) + " " + unitMoneyAr.get(ar[0]);
        }
        return full;
    }

    public boolean isNum(String nsw) {
        for (int i = 0; i < nsw.length(); i++) {
            if (Character.isDigit(nsw.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public String readDURA(String nsw) {
        String full = "___";
        return full;
    }

    public String readNONE(String nsw) {
        String full = "XXX";
        return full;
    }

    public String standard(String s) {

        String in = s.replace(" ", "");
        StringBuilder sa = new StringBuilder(in);
        int i = 0;
        while ((sa.charAt(i) == '0') &&
                (i < sa.length() - 1)) {
            i++;
        }

        sa.replace(0, i, "");

        if (sa.length() % 3 == 1) {
            sa.insert(0, "00");
        } else if (sa.length() % 3 == 2) {
            sa.insert(0, "0");
        }

        return sa.toString();
    }

    public String group(int i) {
        String re = "";
        if (i % 3 == 1) {
            re = " nghìn ";
        } else if (i % 3 == 2) {
            re = " triệu ";
        } else {
            if (i >= 3) {
                re = " tỉ ";
            } else {
                re = "";
            }
        }
        return re;
    }

    public String readNum(String s) {
        if (s.equals("")) {
            return "không";
        } else {
            String result = "";
            level = new Vector<ThreeDigital>();
            String sb = standard(s);
            for (int i = (sb.length() / 3); i > 0; i--) {
                int three = (int) sb.charAt(3 * i - 1) - 48;
                int two = (int) sb.charAt(3 * i - 2) - 48;
                int one = (int) sb.charAt(3 * i - 3) - 48;
                level.add(new ThreeDigital(one, two, three));
            }
            result += level.elementAt(level.size() - 1).read2() + group(level.size() - 1);
            for (int i = level.size() - 2; i >= 0; i--) {
                if (level.elementAt(i).check()) {
                    result += level.elementAt(i).read1() + group(i);
                } else if (i % 3 == 0) {
                    result += group(i);
                }
            }

            return result;
        }
    }

    public String readNNUM(String s) {
        String full = "";
        if (s.contains(".") || s.contains(",")) {
            String sp[] = s.split("[\\.,]");
            if (sp[sp.length - 1].length() < 3) {
                String num = "";
                for (int i = 0; i < sp.length - 1; i++) {
                    num += sp[i];
                }
                full = readNum(num) + " phẩy " + readNum(sp[sp.length - 1]);
            }
        } else {
            s = s.replaceAll("[./]", "");
            full = readNum(s);
        }
        return full;
    }


    //ham dung de doc gio
    public String readNTIM(String s) {
        String res = "";
        String[] sp = s.split(":");

        if (sp.length == 2) {
            res = readNum(sp[0]) + " giờ " + readNum(sp[1]) + "phút ";
        } else if (sp.length == 3) {
            res = readNum(sp[0]) + " giờ " + readNum(sp[1]) + "phút " + readNum(sp[2]) + " giây ";
        }
        return res;
    }

    //ham dung de doc ngay thang (DATE)
    public String readNDAT(String s) {
        String res = "";
        String[] sp = s.split("[/-]");
        if (sp.length == 4) {
            String ngaytruoc = "";
            String ngaysau = "";

            if (sp[0].length() == 1) {
                ngaytruoc = "mồng " + readNum(sp[0]);

            } else {
                ngaytruoc = readNum(sp[0]);
            }
            if (sp[1].length() == 1) {

                ngaysau = "mồng " + readNum(sp[1]);
            } else {
                ngaysau = readNum(sp[1]);
            }
            res = ngaytruoc + " đến ngày" + ngaysau + " tháng " + readNum(sp[2]) + " năm " + readNum(sp[3]);

        } else {
            if(sp.length==3){
               res = readNum(sp[0]) + " tháng " + readNum(sp[1]) + " năm " + readNum(sp[2]);
            if (sp[0].length() == 1 || sp[0].charAt(0) == '0') {
                res = "mồng " + res;
            } 
            }else{
                res=readNDIG(s);
            }
            
        }

        return res;
    }
    //ham dung de doc ngay (DAY)

    public String readNDAY(String s) {
        String res = "";
        String[] sp = s.split("[/-]");
        if (sp.length == 3) {
            String ngaytruoc = "";
            String ngaysau = "";
            if (sp[0].length() == 1) {
                ngaytruoc = "mồng " + readNum(sp[0]);
            } else {
                ngaytruoc = readNum(sp[0]);
            }
            if (sp[1].length() == 1) {
                ngaysau = "mồng " + readNum(sp[1]);
            } else {
                ngaysau = readNum(sp[1]);
            }
            res = ngaytruoc + " đến ngày" + ngaysau + " tháng " + readNum(sp[2]);
        } else {
            res = readNum(sp[0]) + " tháng " + readNum(sp[1]);
            if (sp[0].length() == 1) {
                res = "mồng " + res;
            }

        }


        return res;
    }
    //ham dung de doc thang (MONTH)

    public String readNMON(String s) {
        String res = "";
        String[] sp = s.split("[/-]");
        if (sp.length == 3) {
            res = readNum(sp[0]) + " đến tháng " + readNum(sp[1]) + "năm" + readNum(sp[2]);
        } else {
            res = readNum(sp[0]) + " năm " + readNum(sp[1]);
        }

        return res;
    }

    public void split(String nsw, ArrayList ar) {
        Pattern SPLIT = Pattern.compile("(\\d[\\p{P}]+)|([\\p{P}]+\\d)");

        Matcher m = SPLIT.matcher(nsw);
        int lastIndex = 0;
        if (m.find()) {
            String sNsw = nsw.substring(lastIndex, m.start() + 1);
            ar.add(sNsw);
            nsw = nsw.substring(m.end() - 1);
            split(nsw, ar);
        } else {
            ar.add(nsw);
        }

    }
}
